﻿using System;
using System.Reflection;
using System.ComponentModel;
using BizControl.Library.Attributes;

namespace BizControl.Library.Helpers
{
  public static class EnumHelper
  {
    public static string GetDescription(this Enum currentEnum)
    {
      string description = String.Empty;

      FieldInfo fi = currentEnum.GetType().GetField(currentEnum.ToString());
      DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
      if (da != null)
        description = da.Description;
      else
        description = currentEnum.ToString();

      return description;
    }

    public static string GetLocalizedDescription(this Enum currentEnum)
    {
      string description = String.Empty;

      FieldInfo fi = currentEnum.GetType().GetField(currentEnum.ToString());
      DescriptionAttribute da = (LocalizableDescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(LocalizableDescriptionAttribute));
      if (da != null)
        description = da.Description;
      else
        description = currentEnum.ToString();

      return description;
    }
  }
}
